package com.zx.map.service.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;
import com.zx.map.dao.NoticeLogDao;
import com.zx.map.dao.model.NoticeLog;
import com.zx.map.dao.model.NoticeLogExample;
import com.zx.map.service.NoticeLogService;
import com.zx.map.service.domain.NoticeLogDomain;
import com.zx.map.service.model.NoticeLogModel;
import com.zx.platform.utils.PmpUtils;

/**
 * Created by IntelliJ IDEA. User: Administrator Date: 14-3-1 Time: 上午11:07 To
 * change this template use File | Settings | File Templates.
 */
@Component("noticeLogService")
public class NoticeLogServiceImpl implements NoticeLogService {

    @Autowired
    private NoticeLogDao dao;

    /**
     * 根据Model查找列表
     */
    public List<NoticeLogModel> getListByModel(NoticeLogModel model) throws DataAccessException {
        NoticeLogExample example = new NoticeLogExample();
        NoticeLogExample.Criteria criteria = example.createCriteria();
        String today = PmpUtils.dateToString(new Date(), "yyyy-MM-dd");
        criteria.andBeginDateLessThanOrEqualTo(today);
        criteria.andEndDateGreaterThanOrEqualTo(today);
        //criteria.andEffectiveDateEqualTo(today);
        List<NoticeLog> list = dao.getListByExample(example);
        List<NoticeLogModel> modelList = new ArrayList<NoticeLogModel>();
        for (NoticeLog o : list) {
            NoticeLogModel temp = new NoticeLogModel();
            BeanUtils.copyProperties(o, temp);
            modelList.add(temp);
        }
        return modelList;
    }

    /**
     * 新增
     */
    public String add(NoticeLogModel model) throws DataAccessException {
        NoticeLog temp = new NoticeLog();
        BeanUtils.copyProperties(model, temp);
        String key = dao.add(temp);
        return key;
    }

    /**
     * 修改
     */
    public String update(NoticeLogModel model) throws DataAccessException {
        NoticeLog temp = new NoticeLog();
        BeanUtils.copyProperties(model, temp);
        dao.update(temp);
        return model.getNoticeId().toString();
    }

    /**
     * 删除
     */
    public void delete(NoticeLogModel model) throws DataAccessException {
        NoticeLog temp = new NoticeLog();
        BeanUtils.copyProperties(model, temp);
        dao.delete(temp);
    }

    /**
     * 分页查询
     *
     * @param pageSize
     * @param pageNum
     * @throws DataAccessException
     */
    public Map getPageByModel(int pageSize, int pageNum, NoticeLogDomain domain)
            throws DataAccessException {
        Map<String, Object> map = new HashMap<String, Object>();
        NoticeLogExample example = new NoticeLogExample();
        NoticeLogExample.Criteria criteria = example.createCriteria();
        //排序
        example.setOrderByClause("create_Date desc");
        int pageBegin = (pageNum - 1) * pageSize;
        List<NoticeLog> list = dao.getPageByExample(pageSize, pageBegin, example);
        List<NoticeLogModel> noticeLogModelList = new ArrayList<NoticeLogModel>();
        for (NoticeLog o : list) {
            NoticeLogModel temp = new NoticeLogModel();
            BeanUtils.copyProperties(o, temp);
            noticeLogModelList.add(temp);
        }
        int count = dao.countByExample(example);
        map.put("count", count);
        map.put("list", noticeLogModelList);
        return map;
    }
    
}
